#include "code_window.h"
#include "ui_code_window.h"

#include <Qsci/qsciscintilla.h>

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QMessageBox>

code_window::code_window(QString *second_code, QString *code_tmp1, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::code_window)
{
    ui->setupUi(this);

    setWindowTitle("Code Window");
    setAttribute(Qt::WA_ShowModal, true);   // 设置模态窗口
    setAttribute(Qt::WA_DeleteOnClose);     // 关闭窗口时触发析构函数

    this->second_code_inode = second_code;
    this->code1 = code_tmp1;//存自动生成的代码

    this->init_Qsci();


}

code_window::~code_window()
{
    save_code();
    delete ui;
}

void code_window::init_Qsci()
{
    /// Code Edit
    code_edit_editext->setLexer(code_edit_textLexer);

    code_edit_editext->setMarginType(0, QsciScintilla::NumberMargin);
    code_edit_editext->setMarginLineNumbers(0, true);
    code_edit_editext->setMarginWidth(0, 30);

    ui->gridLayout_code_edit->addWidget((QWidget *)code_edit_editext);

    code_edit_editext->setText(*second_code_inode);
    ///---------------------------------------------------------------

    /// Code View
    code_view_editext->setLexer(code_view_textLexer);

    code_view_editext->setMarginType(0, QsciScintilla::NumberMargin);
    code_view_editext->setMarginLineNumbers(0, true);
    code_view_editext->setMarginWidth(0, 30);

    code_view_editext->setReadOnly(true);   // 设置只读模式
    code_view_editext->setText(*code1 + "\n");//代码前半段
    code_view_editext->append("//待输入的代码\n");
    code_view_editext->append("endmodule");//代码后半段
    ui->gridLayout_code_view->addWidget((QWidget *)code_view_editext);

    view_code();
    ///---------------------------------------------------------------
}

void code_window::save_code()
{//保存代码
    if (this->second_code_inode == nullptr)
    {
        QMessageBox::information(this, "Error", "Error Inode", QMessageBox::Ok);
        this->close();
    }

    //QMessageBox::information(this, "in", editext->text(), QMessageBox::Ok);
    this->second_code_inode->clear();
    this->second_code_inode->append(code_edit_editext->text());

    code_view_editext->setText(*code1);//代码前半段
    code_view_editext->append(*second_code_inode + "\n");
    code_view_editext->append("endmodule");//代码后半段
}

void code_window::view_code()
{
    ;;
}

void code_window::on_pushButton_save_clicked()
{
    this->save_code();
}


void code_window::on_tabWidget_currentChanged(int index)
{//切换编辑/显示界面时自动保存代码
    save_code();
    //QMessageBox::information(this, "Error", QString::number(index), QMessageBox::Ok);
}

